<template>
    <div class="main-layout">
        <div class="banner">
            <div class="box">
                <div class="all">
                    <div class="left">
                    </div>
                    <div class="right">
                        <p>好大夫医疗管理系统</p>
                        <span>用户名</span><el-input autocomplete="" v-model="query.id" placeholder="请输入用户名"></el-input>
                        <span>密&nbsp;&nbsp;码</span> <el-input autocomplete="" v-model="query.password" type="password"
                            placeholder="请输入密码"></el-input>
                        <el-checkbox v-model="remember">记住密码</el-checkbox>
                        <el-button @click="loginEvt">登录</el-button>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>

<script>
import { getUserInfoApi, loginApi } from '@/apis/loginApi';
import { LOGIN_FLAG } from '@/config/common.conf';
import { deCodeApi, enCodeApi, md5Api } from '@/util';
export default {
    data() {
        return {
            query: {
                "id": "",
                "password": ""
            },
            remember: false
        }
    },
    created() {
        let cache = localStorage.getItem('login-info')
        if (!!cache) {
            try {
                cache = deCodeApi(cache).split('****')
                this.query.id = cache[0]
                this.query.password = cache[1]
                this.remember = true
            } catch {
                localStorage.removeItem('login-info')
            }
        }
    },
    methods: {
        async loginEvt() {
            let result = await loginApi({
                ...this.query,
                password: md5Api(this.query.password)
            })
            if (result.code !== 200) {
                this.$message.error('登录失败，原因' + result.message)
                return
            }
            sessionStorage.setItem(LOGIN_FLAG, enCodeApi(result.token + '****' + Date.now()))
            this.$store.commit('common/setToken', result.token)
            let user = await getUserInfoApi(this.query.id)
            if (user.code === 200) {
                this.$store.commit('common/setUserInfo', user.data)
            }
            // 判断用户是否记住密码
            if (this.remember) {
                localStorage.setItem('login-info', enCodeApi(this.query.id + '****' + this.query.password))
            } else {
                localStorage.removeItem('login-info')
            }

            setTimeout(() => {
                this.$router.push('/home')
            })

        }
    }
}
</script>
<style lang="less" scoped>
@import '../../style/common.less';

.main-layout {
    height: 100%;
    position: relative;
    >.banner {
        display: block;
        position: absolute;
        top: 5%;
        left: 4%;
        width: 1422px;
        height: 738px;

        >.box {
            display: block;
            position: relative;
            height: 738px;
            padding-left: 200px;

            &::before {
                content: '';
                display: block;
                position: absolute;
                top: 0;
                left: 0;
                width: 1422px;
                height: 738px;
                background: url(../../../public/banner.png);
                left: 50%;
                top: 50%;
                transform: translate(-50%, -50%);
            }

            >.all {
                position: relative;

                >.left {
                    &::before {
                        content: '';
                        display: block;
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 341px;
                        height: 386px;
                        background: url(../../../public/picture.png)center center / 100% 100% no-repeat;
                        left: 21.5%;
                        top: 50%;
                        transform: translate(-49%, 39%);
                    }
                }

                >.right {
                    display: block;
                    position: absolute;
                    top: 0;
                    left: 0;
                    z-index: 9;
                    width: 477px;
                    height: 383px;
                    background: @g02;
                    border-top-right-radius: 15px;
                    border-bottom-right-radius: 15px;
                    left: 56%;
                    top: 50%;
                    transform: translate(-49%, 39%);

                    >p {
                        margin-top: 15px;
                        font-size: 18px;
                        font-weight: 800;
                        padding-bottom: 20px;
                        color: @g07;
                        text-align: center;
                    }

                    >span {
                        font-size: 16px;
                        margin-left: 50px;
                    }

                    >.el-input {
                        padding-left: 20px;
                        padding-bottom: 65px;
                        margin-right: 50px;
                        height: 30px;
                        line-height: 30px;
                        height: 25px;
                        width: 300px;

                        .el-input__inner {
                            height: 20px;
                        }
                    }

                    >.el-checkbox {
                        padding-left: 50px;
                        margin-right: 500px
                    }

                    >.el-button {
                        font-size: 14px;
                        border-radius: 4px;
                        background: @c05;
                        width: 280px;
                        color: white;
                        margin-left: 118px;
                        margin-top: 30px;
                        height: 40px;
                    }

                    .el-button:focus,
                    .el-button:hover {
                        background-color: #36cfc9;
                    }


                }
            }
        }
    }
}
</style>